package br.com.localfloripa.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import br.com.localfloripa.dao.PerfilDao;
import br.com.localfloripa.entities.Perfil;

@Component("perfilConverter")
public class PerfilConverter implements Converter {
	
	private static final Logger log = LoggerFactory.getLogger(PerfilConverter.class);

	@Inject
	@Named("perfilDao")
	private PerfilDao perfilDao;
		
	@Override
	public Object getAsObject(FacesContext context, UIComponent ui, String value) {
		Perfil perfil = null;
		try {
			perfil = perfilDao.findById(Long.parseLong(value));
		} catch (NumberFormatException e) {
			log.error(e.getMessage(),e);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}		
		return perfil;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent ui, Object object) {		
		return String.valueOf(((Perfil)object).getId());
	}

}
